package com.baosight.sczx.zm.cm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cm.domain.ZMCMDepotorganizationInfo;


/**材料使用管理---库房信息管理---从表部门权限管理
 * <p>Title: ServiceZMCM06.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-5-30 上午10:45:01  余虎  Create
 *</pre>
 */
public class ServiceZMCM11 extends ServiceEPBase {


	/**数据初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM06</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午10:46:00 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cm.department", "orgCode", "orgName", false));
		return outInfo;
	}
	
	/**查询信息
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM06</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午10:46:01 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdDepotId = inInfo.getString("fdDepotId");
		StaticTools.setQueryParameters(inInfo, "fdDepotId", fdDepotId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCMDepotorganizationInfo.query", new ZMCMDepotorganizationInfo());
		return outInfo;
	}


	/**部门权限信息新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM06</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午10:46:01 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		boolean validate = this.validate(inInfo, "ZMCMDepotorganizationInfo.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			if(inInfo.getBlock("detailsResult") == null){
				inInfo.addBlock("detailsResult");
			}
			
			for(int i = 0; i < inInfo.getBlock("detailsResult").getRowCount(); i++){
				String fdDepotId = inInfo.getString("fdDepotId");
				String fdManagmentPermission = inInfo.addBlock("detailsResult").getCellStr(i, "fdManagmentPermission");
				if(fdManagmentPermission == null || fdManagmentPermission.equals("")){
					fdManagmentPermission = "0";
				}
				
				inInfo.getBlock("detailsResult").setCell(i, "fdManagmentPermission", fdManagmentPermission);
				inInfo.getBlock("detailsResult").setCell(i, "fdDepotId", fdDepotId);//主表ID
				String fdKeyId = TablePk.getInstance().getNextValue("T_CM_DEPOTORGANIZATION_INFO");
				inInfo.getBlock("detailsResult").setCell(i, "fdKeyId", fdKeyId );//主键
			}
			
			EiInfo outInfo = super.insert(inInfo, "ZMCMDepotorganizationInfo.insert", "detailsResult");
			return outInfo;
		}	
	}
	
	/**修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM06</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午10:46:01 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		return super.update(inInfo);
	}

	/**删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM06</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午10:46:00 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCMDepotorganizationInfo.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: regainDepartment</p>
	 * <p>Description: 获取部门，以保证一个部门只能对应一个库房</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-19上午10:19:58 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public EiInfo regainDepartment(EiInfo inInfo) {
		//获取所有部门的ID
		EiBlock allBlock = Department.getInstance().getConstructionUnit("cmes.cm.department", "orgCode", "orgName", false);
		List<String> allIdList = allBlock.getCol("orgCode");
		
		//获取已经拥有库房权限的部门的ID
		EiInfo depotInfo = super.query(inInfo, "ZMCMDepotorganizationInfo.query", new ZMCMDepotorganizationInfo());
		List<String> departIdList = depotInfo.getBlock("result").getCol("fdOrgId");
		
		//如果有拥有权限的部门
		if(departIdList !=null){
            //得到没有库房权限的所有部门ID
			allIdList.removeAll(departIdList);
		}
		
		//构造没有库房权限的Block
		EiInfo outInfo = new EiInfo();
		EiBlock block = new EiBlock("cmes.cm.department");

		EiColumn eiColumn = new EiColumn("orgCode");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("orgName");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map> _result = new ArrayList<Map>();
		Map _map = null;
		for(String orgCode : allIdList){
			_map = new HashMap();
			_map.put("orgCode", orgCode);
			_map.put("orgName", Department.getInstance().getDepartmentNameById(orgCode));
			_result.add(_map);
		}

		block.setRows(_result);
		
		outInfo.addBlock(block);
		
		return outInfo;
	}
	
	/**
	 * 部门重复校验
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1下午08:29:16 余虎
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdOrgId = inInfo.getBlock("detailsResult").getCellStr(0, "fdOrgId");
		String fdDepotId = inInfo.getString("fdDepotId");
		params.put("fdOrgId", fdOrgId);
		params.put("fdDepotId",fdDepotId);
		
		List result = dao.query(sqlName, params);
		
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("提交失败，该库房下部门重复 ！");
			return true;
		}
		return false;
	}
}
